package com.project.frba.bi.queueing;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapperResultSetExtractor;

import com.project.frba.bi.descriptor.Descriptor;
import com.project.frba.bi.queueing.mappers.CubeRowMapper;
import com.project.frba.bi.queueing.mappers.PropertyValueRowMapper;
import com.project.frba.bifront.client.model.Cube;
import com.project.frba.bifront.client.model.CubeDataRow;

public class OlapDAO extends AbstractDAO {
	
	@SuppressWarnings("unchecked")
	public Cube getData(String query, List<String> properties, Descriptor descriptor) {
		Cube cubeData = new Cube(properties);
		cubeData.setSql(query);
		
		RowMapperResultSetExtractor extractor = new RowMapperResultSetExtractor(new CubeRowMapper(descriptor));
		List<CubeDataRow> rows = (List<CubeDataRow>)jdbcTemplate.query(query, extractor);
		cubeData.addAllDataRows(rows);
		
		return cubeData;
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getValuesForProperty(String query, Class propertyType) {
		RowMapperResultSetExtractor extractor = new RowMapperResultSetExtractor(new PropertyValueRowMapper(propertyType));
		List<String> values = (List<String>)jdbcTemplate.query(query, extractor);
		List<String> stringValues = new ArrayList<String>();
		for (Object object : values) {
			stringValues.add(object.toString());
		}
		
		return stringValues;
		
	}

	public void executeDDLStatement(String statement) {
		jdbcTemplate.execute(statement);
	}
	
	public void executeBatch(String[] sql){
		jdbcTemplate.batchUpdate(sql);
	}
	
}
